package jiyun.com.yiyanmeng;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;

import java.util.ArrayList;

import utils.ToastUtil;


public class ReceivingActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mImageBack;
    private TextView mTvTitle;
    private TextView mTvName;
    private TextView mTvChoose;
    private EditText mEtName;
    private TextView mTvTel;
    private EditText mEtTel;
    private TextView mRegion;
    private TextView mTvRegion;
    private ImageView mIvRight;
    private EditText mEtAddress;
    private TextView mTvDelAdd;
    CityPickerView mPicker=new CityPickerView();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiving);
        initview();
        mPicker.init(this);

    }



    private void initview() {
        mImageBack = findViewById(R.id.image_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvName = findViewById(R.id.tv_name);
        mTvChoose = findViewById(R.id.tv_choose);
        mEtName = findViewById(R.id.et_name);
        mTvTel = findViewById(R.id.tv_tel);
        mEtTel = findViewById(R.id.et_tel);
        mRegion = findViewById(R.id.region);
        mTvRegion = findViewById(R.id.tv_address);
        mIvRight = findViewById(R.id.iv_right);
        mEtAddress = findViewById(R.id.et_address);
        mTvDelAdd = findViewById(R.id.tv_delAdd);
        mTvDelAdd.setOnClickListener(this);
        //选择地区
        mTvRegion.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_delAdd:
                String name = mEtName.getText().toString();  //名字
                String phone = mEtTel.getText().toString();  //手机号
                //  mTvRegion  选项地址
                String detailed = mTvRegion.getText().toString();
                //详细地址
                String address = mEtAddress.getText().toString();
                Intent intent = new Intent(this, SiteActivity.class);
                intent.putExtra("name",name);
                intent.putExtra("phone",phone);
                intent.putExtra("address",address);
                intent.putExtra("detailed",detailed);
                setResult(2, intent);
                finish();
                break;

            case R.id.tv_address:
                selectAddress();
                break;
            case R.id.image_back:
                finish();
                break;
        }
    }
    private void selectAddress() {
//添加默认的配置，不需要自己定义
        CityConfig cityConfig = new CityConfig.Builder().build();
        mPicker.setConfig(cityConfig);
//监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener(){

            private String districtName;
            private String cityName;
            private String name;

            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {

                //省份
                if (province != null) {
                    name = province.getName();



                }

                //城市
                if (city != null) {
                    cityName = city.getName();

                }

                //地区
                if (district != null) {
                    districtName = district.getName();

                }
                mTvRegion.setText(name+" "+cityName+" "+districtName);



            }

            @Override
            public void onCancel() {
                ToastUtil.show(ReceivingActivity.this,"取消");
            }
        });

        //显示
        mPicker.showCityPicker( );
    }

}
